package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.util.RequiredPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    @RequiredPermission(name = "部门页面",expremission = "department:list")
    @RequestMapping("/list")
    public String list(Model model, QueryObject qo){//因为要看到他的数据,所以要放在作用域里面,所以得用到它的addattribute
        model.addAttribute("pageInfo",departmentService.query(qo));      //因为要用到分页所以这里查询全部的就不要了
        /*List<Department> departments = departmentService.listall();
            model.addAttribute("depatrments",departments);*/
            return "department/list";//因为已经配置了视图解析器,所以省略了前面的WEB-INF/views/ 和后面的.jsp
    }

    @RequiredPermission(name = "部门删除",expremission = "department:delete")
    @RequestMapping("/delete")
    public String delete(Long id){
        if(id !=null){
            departmentService.delete(id);
        }
        return "redirect:/department/list";//就像以前一样,删除后要除定向会list查找全部的页面
    }


    @RequestMapping("/input")
    public String input(Long id,Model model){
        if (id !=null){
            model.addAttribute("department",departmentService.get(id));
        }
        return "department/input";
    }

    @RequiredPermission(name = "部门新增/编辑",expremission = "department:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Department department){
            if(department.getId()!=null){//判断你进来的是否带有id,如果带有id的就肯定是修改的了
                departmentService.update(department);
            }
            else {
                departmentService.save(department);
            }
        return "redirect:/department/list";
    }
}
